// 请求数据 渲染
function printing() {
    $.ajax({
        url: '/admin/comments',
        success: function (res) {
            console.log(res);
            if (res.status == 0) {
                let sum = ''
                res.data.forEach(item => {
                    // 分割字符串
                    let data = item.cdate.split('T')[0]
                    sum += `
                    <tr>
                    <td>${item.id}</td>
                    <td>${item.uname}</td>
                    <td>${item.content}</td>
                    <td>${data}</td>
                    <td><button data-id="${item.id}" class="layui-btn layui-btn-sm layui-btn-danger delete">删除</button></td>
                    </tr>
                    `
                })
                $('tbody').html(sum)
            }
        }
    })
}
printing()
let layer = layui.layer
// 点击删除
$('tbody').on('click', '.delete', function () {
    // 获取id
    let id = $(this).attr('data-id')
    // 通过id删除评论
    $.ajax({
        url: '/admin/comments/' + id,
        type: 'delete',
        success: function (res) {
            if (res.status == 0 && res.message == '删除评论成功') {
                layer.msg(res.message)
                printing()
            } else {
                layer.msg(res.message)
            }
        }
    })
})